package com.example.lzl.jisuanqi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.lang.StringBuffer;

/**
 * Created by lzl on 2016/10/17.
 */
public class jinzhiActivity extends AppCompatActivity {
    Button btn_back;
    Button btn_zhuan2;
    Button btn_zhuan16;
    EditText ed_input,ed_show;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jinzhi_layout);
        btn_back= (Button) findViewById(R.id.button_back);
        btn_zhuan2= (Button) findViewById(R.id.button_erjinzhi);
        btn_zhuan16= (Button) findViewById(R.id.button_shiliujinzhi);
        ed_input= (EditText) findViewById(R.id.ed_input);
        ed_show= (EditText) findViewById(R.id.ed_show);

        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(jinzhiActivity.this,MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
        btn_zhuan2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=ed_input.getText().toString();
                int d=Integer.parseInt(str);
                erjinzhi(d);
            }
        });
        btn_zhuan16.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=ed_input.getText().toString();
                int d=Integer.parseInt(str);
               shiliujinzhi(d);
            }
        });
    }
    public void erjinzhi(int n) {
        if(n==0){
            ed_show.setText("0");
        }
        StringBuffer sb=new StringBuffer();
        for(int i=n;i>0;i=i/2){
            sb.append(i%2>0?'1':'0');
        }
        String reverse = sb.reverse().toString();
        ed_show.setText(reverse);
    }
    public void shiliujinzhi(int n){
        if(n==0){
            ed_show.setText("0");
        }
        StringBuffer sb=new StringBuffer();
        for(int i=n;i>0;i=i/16){
            int j=i%16;
            if(j<10){
                sb.append(String.valueOf(j));
            }else{
                switch (j){
                    case 10:sb.append("A");break;
                    case 11:sb.append("B");break;
                    case 12:sb.append("C");break;
                    case 13:sb.append("D");break;
                    case 14:sb.append("E");break;
                    case 15:sb.append("F");break;
                }
            }
        }
        sb.append("x");
        sb.append("0");
        String reverse = sb.reverse().toString();
        ed_show.setText(reverse);
    }
}
